package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.DailyMetricsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.DailyMetricsForm;
import com.youlai.boot.system.model.query.DailyMetricsQuery;
import com.youlai.boot.system.model.vo.DailyMetricsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 全年销售数据前端控制层
 *
 * @author xiheforever
 * @since 2025-05-11 13:29
 */
@Tag(name = "全年销售数据接口")
@RestController
@RequestMapping("/api/v1/daily-metrics")
@RequiredArgsConstructor
public class DailyMetricsController  {

    private final DailyMetricsService dailyMetricsService;

    @Operation(summary = "全年销售数据分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:daily-metrics:query')")
    public PageResult<DailyMetricsVO> getDailyMetricsPage(DailyMetricsQuery queryParams ) {
        IPage<DailyMetricsVO> result = dailyMetricsService.getDailyMetricsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增全年销售数据")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:daily-metrics:add')")
    public Result<Void> saveDailyMetrics(@RequestBody @Valid DailyMetricsForm formData ) {
        boolean result = dailyMetricsService.saveDailyMetrics(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取全年销售数据表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:daily-metrics:edit')")
    public Result<DailyMetricsForm> getDailyMetricsForm(
        @Parameter(description = "全年销售数据ID") @PathVariable Long id
    ) {
        DailyMetricsForm formData = dailyMetricsService.getDailyMetricsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改全年销售数据")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:daily-metrics:edit')")
    public Result<Void> updateDailyMetrics(
            @Parameter(description = "全年销售数据ID") @PathVariable Long id,
            @RequestBody @Validated DailyMetricsForm formData
    ) {
        boolean result = dailyMetricsService.updateDailyMetrics(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除全年销售数据")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:daily-metrics:delete')")
    public Result<Void> deleteDailyMetricss(
        @Parameter(description = "全年销售数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = dailyMetricsService.deleteDailyMetricss(ids);
        return Result.judge(result);
    }
}
